package simpleGeom;

import processing.core.PGraphics;

public class Edge implements HasLine2D{
HasPos2D p1;
HasPos2D p2;
public Edge(HasPos2D p1,HasPos2D p2){
	this.p1=p1;
	this.p2=p2;
}
public Edge(float x1,float y1,float x2,float y2){
	this.p1=new Vec2D(x1,y1);
	this.p2=new Vec2D(x2,y2);
}
public Edge(){
	
}
public float getX1(){
	return p1.getX();
}
public float getY1(){
	return p1.getY();
}
public float getX2(){
	return p2.getX();
}
public float getY2(){
	return p2.getY();
}
public Vec2D getVector(){
	return Vec2D.sub(p2, p1);
}

public Vec2D getCenter(){
	Vec2D c=Vec2D.add(p1, p2);
	c.mult(0.5f);
	return c;
}
public float getAngle(){
	Vec2D v=getVector();
	return v.getAngle();
}
public float getLength(){
	return Vec2D.dist(p1, p2);
}
public int  getNSegmentsMinLength(float minL){
	int n=(int)(getLength()/minL);
	return Math.max(1,n);
}
public Vec2D[] getSegments(int n){
	Vec2D[] segments=new Vec2D[n+1];
	Vec2D v=getVector();
	v.div(n);
	for (int i=0;i<n+1;i++){
		Vec2D p=Vec2D.mult(v, i);
		p.add(p1);
		segments[i]=p;
	}
	return segments;
}
public Vec2D getPointAbs(float dist){
	Vec2D v=getVector();
	v.setLength(dist);
	v.add(p1);
	return v;
}
public HasPos2D getP1() {
	return p1;
}
public void setP1(HasPos2D p1) {
	this.p1 = p1;
}
public HasPos2D getP2() {
	return p2;
}
public void setP2(HasPos2D p2) {
	this.p2 = p2;
}
public void display(PGraphics g){
	g.line(getX1(), getY1(), getX2(), getY2());
}
public  Edge getOffsetEdge(
		float offset) {
	Vec2D v=Vec2D.sub(getP2(), getP1());
	v.setLength(offset);
	v.rot90m();
	Vec2D p1Offset = Vec2D.add(p1, v);
	Vec2D p2Offset = Vec2D.add(p2, v);
	return new Edge(p1Offset, p2Offset);
}
}
